<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ItemManagement extends MY_Controller {

	function __construct() {
		parent::__construct();
	}
	
	public function index($offer_id)
	{
		if($this->LoggedIn()){
			
			$this->load->model('offer_model');
			
			if($offer_id == 0){
				$offer = array(
					'offer_id' => $offer_id,
					'item_name' => '',
					'item_price' => 0,
					'item_condition' => 3,
					'age_year' => 0,
					'age_month' => 0,
					'item_description' => '',
					'item_pic' => 'default.png',
					'fb_id' => '',
					'category_id' => '',
					'sub_category_id' => '',
				);
			}
			else{				
				$offer = $this->offer_model->get_offer_by_offer_id($offer_id);
				//Check user
				if ($this->fb_id != $offer['fb_id']){ 
					$this->add_status_message('You are not allowed to edit the information about this item!');
					redirect('/home','refresh');
				}
			}
			$data['offer'] = $offer;
			
			//Submit form (add new item or edit existing item)
			if ($this->input->post('submit')) {
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color: red; font-weight: bold;">', '</span>');
				$this->form_validation->set_rules('item_name', 'Item Name', 'trim|required');
				$this->form_validation->set_rules('item_condition', 'Item Condition', 'required');
				$this->form_validation->set_rules('age_year', 'Age Year', 'required');
				$this->form_validation->set_rules('age_month', 'Age Month', 'required');
				$this->form_validation->set_rules('category_id', 'Category', 'required');
				$this->form_validation->set_rules('sub_category_id', 'Subcategory', 'required');
				$this->form_validation->set_rules('item_price', 'Item Price', 'callback_is_price');
				
				//To save the image in case validation fails
				if($this->input->post('item_pic')){
					$data['offer']['item_pic'] = $this->input->post('item_pic');
				}
				if ($this->form_validation->run() == TRUE) {

					$offer = array(
						'item_name' => $this->input->post('item_name'),
						'item_pic' => $this->input->post('item_pic'),
						'item_price' => $this->input->post('item_price'),
						'item_condition' => $this->input->post('item_condition'),
						'item_description' => $this->input->post('item_description'),
						'age_year' => $this->input->post('age_year'),
						'age_month' => $this->input->post('age_month'),
						'category_id' => $this->input->post('category_id'),
						'sub_category_id' => $this->input->post('sub_category_id'),
						'fb_id' => $this->fb_id,
					);
						
					//New Offer
					if ($offer_id == 0) {
						$result = $this->offer_model->insert_offer($offer);
						$this->add_status_message('Your new item has been created successfully!');
						redirect('/home/','refresh');
					}
						
					//Update Offer
					else {
						$this->offer_model->update_offer($offer, $offer_id);
						$this->add_status_message('Your item has been edited successfully!');
						redirect('/itemdescription/' . $data['offer_info']['offer_id'],'refresh');
					}	
				}
			}

			else if ($this->input->post('upload_pic')) {				
				$config = array(
					'allowed_types' => 'jpg|gif|png|jpeg',
					'upload_path' => realpath(APPPATH . '../resources/item_images/'),
					'max_size' => '3072',
					'remove_space' => true,
				);
				$this->load->library('upload', $config);
				
				if (!$this->upload->do_upload()){
					$data['upload_file_error'] = $this->upload->display_errors('<span style="color: red; font-weight: bold;">', '</span>');
					//To save the image in case validation fails
					if($this->input->post('item_pic')){
						$data['offer']['item_pic'] = $this->input->post('item_pic');
					}
				}
				else {
					$image_data = $this->upload->data();
					$config_thumb = array(
									'image_library' => 'gd2',
									'source_image' => $image_data['full_path'],
									'create_thumb' => true,
									'maintain_ratio' => true,
									'thumb_marker' => '',
									'width' => 200,
									'height' => 200,
								);
 					$this->load->library('image_lib', $config_thumb); //load library
					if ( ! $this->image_lib->resize()) {
						$data['upload_file_error'] = $this->image_lib->display_errors('<span style="color: red; font-weight: bold;">', '</span>');
					}
					else{
							rename($image_data['full_path'], $image_data['file_path'] . 'temp_' . $this->fb_id . $image_data['file_ext']);
							$data['offer']['item_pic'] = 'temp_' . $this->fb_id . $image_data['file_ext'];
					}
				}
			}
			
			$this->load->model('category_model');
			$data['categories'] = $this->category_model->get_all_categories();
			$data['sub_categories'] = $this->category_model->get_all_sub_categories();
			
			
			$data['title'] = 'Item Management';
			$data['content_view'] = 'itemmanagement_view';
			$data['stylesheets'] = array('itemmanagement');
			$data['javascripts'] = array('star-rating', 'itemmanagement');
			$data['offer_id'] = $offer_id;
			$this->load_view($data, TRUE);
		}
	}
	
	
	//For form validation
	public function is_price($str)
	{
		$this->form_validation->set_message('is_price', 'Please enter a number/decimal (2dp) smaller than $10000.');
	 
		return (bool) preg_match('/^[0-9]{0,4}+(\.[0-9]{0,2})?$/', $str);
	}
}